// gaiga-temp2550
#include "./USB/usb_function_cdc.h"
#include "GenericTypeDefs.h"
#include "Compiler.h"
#include "usb_config.h"
#include "USB/usb_device.h"
#include "USB/usb.h"
#include "HardwareProfile.h"
#include <delays.h>
#include <adc.h>
#pragma config FOSC=HSPLL_HS, PLLDIV=5, CPUDIV=OSC1_PLL2
#pragma config USBDIV=2, PWRT=ON, FCMEN=ON, IESO=ON
#pragma config BOR=ON, BORV=0, WDT=OFF, LVP=OFF, VREGEN=ON
#pragma config MCLRE=ON, PBADEN=OFF, WDTPS=1024
#pragma config LPT1OSC=OFF, CCP2MX=OFF, DEBUG=OFF
#pragma config CP0=OFF, CP1=OFF, CP2=OFF, CP3=OFF, CPB=OFF
#pragma config CPD=OFF, WRT0=OFF, WRT1=OFF, WRT2=OFF, WRT3=OFF
#pragma config WRTB=OFF, WRTC=OFF, WRTD=OFF, EBTR0=OFF
#pragma config EBTR1=OFF, EBTR2=OFF, EBTR3=OFF, EBTRB=OFF
#define Delay_50uS Delay100TCYx(6)
#define Delay_100uS Delay100TCYx(12)
#define Delay_1mS Delay1KTCYx(12)
#define Delay_2mS Delay1KTCYx(24)
#define Delay_5mS Delay10KTCYx(6)
#define Delay_10mS Delay10KTCYx(12)
#define Delay_20mS Delay10KTCYx(24)
#define Delay_100mS Delay10KTCYx(120)
#pragma udata
unsigned int i, j, jj, T;
unsigned long N;
char buf[12];
char input_buffer[64];
char output_buffer[64];
void YourHighPriorityISRCode();
void YourLowPriorityISRCode();
void ProcessIO(void);
#pragma code REMAPPED_HIGH_INTERRUPT_VECTOR = 0x08
void Remapped_High_ISR (void){
_asm goto YourHighPriorityISRCode _endasm
}
#pragma code REMAPPED_LOW_INTERRUPT_VECTOR = 0x18
void Remapped_Low_ISR (void){
_asm goto YourLowPriorityISRCode _endasm
}
#pragma code
#pragma interrupt YourHighPriorityISRCode
void YourHighPriorityISRCode(){
USBDeviceTasks();
}
#pragma interruptlow YourLowPriorityISRCode
void YourLowPriorityISRCode(){
}
#pragma code
void main(void){
TRISA = 0b00001001; // AN0, VREF+
TRISB = 0b00000001; // RB0
TRISC = 0b00000000;
ADCON0 = 0b00000001;
ADCON1 = 0b00011110; // AN0:Analog, Vss-VREF+
ADCON2 = 0b10100110; // Right,12Tad,1/64
USBDeviceInit();// USB初期化.
N = 0;
PORTBbits.RB1 = 1;
while(1){
/* USB接続完了待ち */
if(USB_BUS_SENSE && (USBGetDeviceState() == DETACHED_STATE))
USBDeviceAttach();
/* デバイス接続状態なら機能実行 */
if((USBDeviceState >= CONFIGURED_STATE)&&(USBSuspendControl!=1)){
ProcessIO();
}
}
}
void ProcessIO(void){
if(getsUSBUSART(input_buffer,20)){
switch(input_buffer[0]) { // 最初の1文字チェック
case '0':
for(j=0;j<2;j++){
if(USBUSARTIsTxTrfReady()) // 送信可能か?
putrsUSBUSART("OK");
CDCTxService();
}
break;
case '1':
for(i=0;i<10000;i++){ // 10000*1mS=10S
if(PORTBbits.RB0){
N++;
PORTBbits.RB1 = 0; // HC74 クリヤ
Delay_100uS; // 0.1mS待つ(入力パルス幅 0.05mS以下)
PORTBbits.RB1 = 1;
}
Delay_1mS; // 1mS待つ(測定時間)
}
N = N * 6; // CPM 換算(60Sec)
buf[4] = (N % 10) + '0'; N = N / 10;
buf[3] = (N % 10) + '0'; N = N / 10;
buf[2] = (N % 10) + '0'; N = N / 10;
buf[1] = (N % 10) + '0'; N = N / 10;
buf[0] = (N % 10) + '0';
N = 0;
buf[5] = ' '; buf[6] = ' ';
SetChanADC(ADC_CH0);
ConvertADC();
while(BusyADC());
T = ADRESH*256 + ADRESL;
buf[10] = (T % 10) + '0'; T = T / 10;
buf[9] = '.';
buf[8] = (T % 10) + '0'; T = T / 10;
buf[7] = (T % 10) + '0';
buf[11] = '\n';
for(jj=0;jj<2;jj++){
if(USBUSARTIsTxTrfReady())
putUSBUSART(buf, 12);
CDCTxService();
}
break;
default:
break;
}
}
}
// ***********************************************************
// ************** USB Callback Functions *********************
// ***********************************************************
/*************************************************************
* Function: void USBCBSuspend(void)
*************************************************************/
void USBCBSuspend(void)
{
}
/*************************************************************
* Function: void USBCBWakeFromSuspend(void)
*************************************************************/
void USBCBWakeFromSuspend(void)
{
}
/*************************************************************
* Function: void USBCB_SOF_Handler(void)
*************************************************************/
void USBCB_SOF_Handler(void)
{
}
/*************************************************************
* Function: void USBCBErrorHandler(void)
*************************************************************/
void USBCBErrorHandler(void)
{
}
/*************************************************************
* Function: void USBCBCheckOtherReq(void)
*************************************************************/
void USBCBCheckOtherReq(void)
{
USBCheckCDCRequest();
}//end
/*************************************************************
* Function: void USBCBStdSetDscHandler(void)
*************************************************************/
void USBCBStdSetDscHandler(void)
{
}//end
/*************************************************************
* Function: void USBCBInitEP(void)
*************************************************************/
void USBCBInitEP(void)
{
CDCInitEP();
}
/*************************************************************
* Function: void USBCBSendResume(void)
*************************************************************/
void USBCBSendResume(void)
{
static WORD delay_count;
if(USBGetRemoteWakeupStatus() == TRUE)
{
//Verify that the USB bus is in fact suspended, before we send
//remote wakeup signalling.
//if(USBIsBusSuspended() == TRUE)
{
USBMaskInterrupts();
//Clock switch to settings consistent with normal USB operation.
USBCBWakeFromSuspend();
USBSuspendControl = 0;
//USBBusIsSuspended = FALSE; //So we don't execute this code again,
//until a new suspend condition is detected.
delay_count = 3600U;
do
{
delay_count--;
}while(delay_count);
//Now drive the resume K-state signalling onto the USB bus.
USBResumeControl = 1; // Start RESUME signaling
delay_count = 1800U; // Set RESUME line for 1-13 ms
do
{
delay_count--;
}while(delay_count);
USBResumeControl = 0; //Finished driving resume signalling
USBUnmaskInterrupts();
}
}
}
/***************************************************************
* Function: void USBCBEP0DataReceived(void)
***************************************************************/
#if defined(ENABLE_EP0_DATA_RECEIVED_CALLBACK)
void USBCBEP0DataReceived(void)
{
}
#endif
/***************************************************************
* Function: BOOL USER_USB_CALLBACK_EVENT_HANDLER(
***************************************************************/
BOOL USER_USB_CALLBACK_EVENT_HANDLER(USB_EVENT event, void *pdata, WORD size)
{
switch(event)
{
case EVENT_TRANSFER:
break;
case EVENT_SOF:
USBCB_SOF_Handler();
break;
case EVENT_SUSPEND:
USBCBSuspend();
break;
case EVENT_RESUME:
USBCBWakeFromSuspend();
break;
case EVENT_CONFIGURED:
USBCBInitEP();
break;
case EVENT_SET_DESCRIPTOR:
USBCBStdSetDscHandler();
break;
case EVENT_EP0_REQUEST:
USBCBCheckOtherReq();
break;
//case EVENT_BUS_ERROR:
//USBCBErrorHandler();
//break;
//case EVENT_TRANSFER_TERMINATED:
//break;
default:
break;
}
return TRUE;
}